import 'package:bot_toast/bot_toast.dart';
import 'package:flutter/cupertino.dart';
import 'package:get/get.dart';

import '../../../utils/search_module.dart';
import 'comment_model.dart';
import 'file_model.dart';
import '../../../services/address.dart';
import '../../../services/dio_manager.dart';
import 'my_mission_detail_model.dart';

class MyMissionDetailController extends GetxController{

  MyMissionDetailModel? model;
  FileModel? fileModel;
  /// 获取数据
  requestData({var id}) async {

    var json = await DioManager()
        .kkRequest("${Address.projectInfo}/$id", method: 'get');
    MyMissionDetailModel _model = MyMissionDetailModel.fromJson(json);
    model = _model;
    update();
  }

  /// 获取附件
  requestDataWithAttachment(var id) async {
    var json = await DioManager().kkRequest(
        '${Address.attachment}/mission.mission/${id}',
        method: 'get');

    FileModel model = FileModel.fromJson(json);
    fileModel = model;
    update();
  }

  ///获取评论
  ///
  CommentModel? commentModel;
  requestDataWithComment(var id) async {
    var json = await DioManager().kkRequest(
        '${Address.commentPList}/${id}',
        method: 'get');
    print('评论json========${json}');

    CommentModel model = CommentModel.fromJson(json);

    commentModel = model;

    update();
  }

  /// 评论
  requestDataWithMessageAdd(var res_id) async {
    var params = {
      'model':'mission.mission',
      'res_id':'${res_id}',
      'content':'${commentTextEditing.text}',
    };
    var json = await DioManager().kkRequest(Address.messageAdd,bodyParams: params);
    BotToast.showText(text: '${json['message']}');
    requestDataWithComment(Get.arguments);
  }

  TextEditingController commentTextEditing = TextEditingController();


  /// 发送评论
  tapClickSendComment(){
    if (commentTextEditing.text.isEmpty) {
      BotToast.showText(text: '评论不能为空');
      return;
    }
    requestDataWithMessageAdd(model?.data?.id);
    // requestDataWithComment();
    requestData(id: Get.arguments);
    commentTextEditing.text = '';
    SearchModule.closeKeyboard(Get.context!);
  }


  @override
  void onInit() {
    // TODO: implement onInit
    super.onInit();

    print('Get.arguments===${Get.arguments}');

    requestData(id: '${Get.arguments}');
    requestDataWithComment(Get.arguments);
  }

  @override
  void onReady() {
    // TODO: implement onReady
    super.onReady();
    // print('Get.arguments===${Get.arguments}');
    //
    // requestData(id: '${Get.arguments}');
    // requestDataWithComment(Get.arguments);


  }

}